<?php

ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Shanghai');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

error_reporting(E_ALL);
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';//downloading
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
include 'PHPExcel/Worksheet/ColumnDimension.php';
$inputFileType = 'Excel5';
//	$inputFileType = 'Excel2007';
//read
$inputFileName = "2013master1.xls";//name of file you‘ll read

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);//选择工作区域
$data = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

$db = mysql_connect('localhost', 'root', '') or

die("Could not connect to database.");//连接数据库

mysql_query("set names utf8");//输出中文

mysql_select_db('lab');       //选择数据库


$lastarea="";
//以下代码是将excel表数据插入到mysql中，根据excel表字段的多少，改写以下代码！ 
foreach ($data as $row_index => $col_array) {
	$sql="INSERT INTO `personalinfo`(ID, name, password, email, phone) VALUES(";
	
	/*for($index="A";$index<"S";++$index)
	{			
		$col_array[$index]=str_replace("'","''",$col_array[$index]);//处理单引号
		$sql.="'$col_array[$index]'";
		if($index!="R")
			$sql.=",";			
	}*/
	
	$sql.=("'".$col_array['A']."',");
	$sql.=("'".$col_array['B']."',");
	$sql.=("'".$col_array['A']."',");
	$sql.=("'".$col_array['E']."',");
	$sql.=("'".$col_array['D']."'");
	
	$sql=$sql.");" ;
	//echo $sql;
	if(mysql_query($sql,$db))
	{
		//echo "Query successfully!"."<br />";
	}
	else
	{
		echo "Error creating database: " . mysql_error()."<br />";
	}

}

/*$objPHPExcel->setActiveSheetIndex(1);//选择工作区域
$data = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
//以下代码是将excel表数据插入到mysql中，根据excel表字段的多少，改写以下代码！ 
foreach ($data as $row_index => $col_array) {
	$sql="INSERT INTO `conferences` VALUES(";
	if(!empty($col_array['A']))
				{
					$lastarea=$col_array['A'];
				}
		$sql.="'$lastarea',";
	for($index="B";$index<"O";++$index)
			{
				$col_array[$index]=str_replace("'","''",$col_array[$index]);//处理单引号
				$sql.="'$col_array[$index]'";
				if($index!="N")
					$sql.=",";
				
			}
	$sql=$sql.");" ;
	echo $sql;
	if(mysql_query($sql,$db))
	{
		//echo "Query successfully!"."<br />";
	}
	else
	{
		echo "Error creating database: " . mysql_error()."<br />";
	}

}*/

?>
